package org.example;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Properties;

public class MyRunnbale implements Runnable{
    public static ArrayList<Socket> sockets=new ArrayList<>();
    private  Socket socket;
    public MyRunnbale(Socket accept) {
        socket=accept;
        sockets.add(socket);
    }

    @Override
    public void run() {
        while (true) {
            try {
                BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                System.out.println("读取客户端传来的数据");
                String s = br.readLine();
                String[] split = s.split("&");
                String inputName=split[0];
                String inputPassword=split[1];
                Properties prop=new Properties();
                FileInputStream fis=new FileInputStream("C:\\Users\\夏明毅\\Desktop\\java课程代码\\SocketDemo\\userInfo.text");
                prop.load(fis);
                System.out.println("服务端获取到的姓名："+inputName);
                System.out.println("服务端获取到的密码："+inputPassword);
                if (split[2].equals("1")){
                    if (prop.containsKey(inputName)){
                        String o =(String) prop.get(inputName);
                        if (o.equals(inputPassword)){
                            sendMessage(bw,"1");
                            while (true){
                                String message = receiveMessage(br, inputName);
                                System.out.println(message);
                                for (int i = 0; i < sockets.size(); i++) {
                                    sendMessage(sockets.get(i),message);
                                }
                            }
                        }else {
                            sendMessage(bw,"3");
                        }
                    }else {
                        sendMessage(bw,"2");
                    }
                }else if (split[2].equals("2")){
                    System.out.println();
//                    注册
                    if (!(inputName.matches("^[a-zA-Z]{6,18}$")&&inputPassword.matches("^[a-zA-Z][0-9]{2,7}$"))){
                        sendMessage(bw,"3");
                        return;
                    }
                    System.out.println(prop.containsKey(inputName));
                    if(prop.containsKey(inputName)){
                        sendMessage(bw,"2");
                    }else {
                        prop.setProperty(inputName,inputPassword);
                        sendMessage(bw,"1");

                    }
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void sendMessage(BufferedWriter bw, String code) throws IOException {
        bw.write(code);
        bw.newLine();
        bw.flush();
    }
    public void sendMessage(Socket socket, String code) throws IOException {
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(code);
        bw.newLine();
        bw.flush();
    }
    public static String receiveMessage(BufferedReader br, String inputName) throws IOException {
        String s = br.readLine();
        String str=inputName+"发送到服务的消息："+s;
        return str;
    }
}
